package com.lagou.controller;

import com.lagou.feign.EmailServiceFeignClient;
import com.lagou.service.CodeService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private CodeService codeService;
    @Autowired
    private EmailServiceFeignClient emailServiceFeignClient;

    /**
     * ⽣成验证码并发送到对应邮箱，成功true，失败 false
     * @param email
     * @return
     */
    @PostMapping("/create/{email}")
    public Boolean createCode(@PathVariable String email) {

        String code = RandomStringUtils.randomAlphanumeric(6);

        codeService.addCode(email, code);

        Boolean result = emailServiceFeignClient.sendEmail(email, code);

        return result;
    }

    /**
     * 0/1/2 校验验证码是否正确0正确1错误2超时
     * @param email
     * @param code
     * @return
     */
    @GetMapping("/validate/{email}/{code}")
    public Integer validateCode(@PathVariable String email,@PathVariable String code) {
        return codeService.validateCode(email,code);
    }
}
